<?php
/**
 * Created by Dumitru Russu
 * User: developer
 * Date: 11/9/12
 * Time: 10:41 AM
 * To change this template use File | Settings | File Templates.
 */
class Db_ReportedProblems extends Common {
	protected static $tableName = TAB_REPORTED_PROBLEMS;
	protected static $dbFields = array(
		'id',
		'rp_project_id',
		'rp_user_id',
		'rp_type',
		'rp_status',
		'rp_session_id',
		'rp_order_id',
		'rp_add_date',
		'rp_email',
		'rp_phone',
		'rp_problem_content',
		'rp_page',
		'rp_memory_list_identifier',
		'rp_user_ip',
		'rp_country',
		'rp_city',
		'rp_os_name',
		'rp_browser',
		'rp_flash_version',
		'rp_screen_resolution',
		'deleted'
	);

	/**
	 * @var Db_ReportedProblems
	 */

	public $objectArray;

	public $id;
	public $rp_project_id;
	public $rp_user_id;
	public $rp_type;
	public $rp_status;
	public $rp_session_id;
 	public $rp_order_id;
 	public $rp_add_date;
	public $rp_email;
	public $rp_phone;
	public $rp_problem_content;
	public $rp_page;
	public $rp_memory_list_identifier;
	public $rp_user_ip;
	public $rp_country;
	public $rp_city;
	public $rp_os_name;
	public $rp_browser;
	public $rp_flash_version;
	public $rp_screen_resolution;
	public $deleted;

	function __construct($value = null, $fieldName = null) {
		parent::__construct($value, $fieldName);
	}

	/**
	 * @static
	 * @param $limit
	 * @param string $sqlSearch
	 * @param string $orderBy
	 * @return mixed
	 */
	public static function getAllReportedProblems($limit, $sqlSearch = '', $orderBy = '') {
		$joinSearch = null;
		if (empty($sqlSearch)) {
			$sqlSearch = '1=1';
			$joinSearch = '';
		} else {
		//	$joinSearch = " LEFT JOIN " . TAB_TRANSLATIONS . " tr ON tr.t_module = pr.pr_module AND tr.t_key = pr.pr_name AND tr.t_lang_id = " . conf('COUNTRY_LANG_ID') ;

		//	$sqlSearch .= " OR (`tr`.`t_content` LIKE " . self::$DB->escapeValue('%' . str_replace('%', "\\%", post('sSearch')) . '%') . ")";
		}
		if(empty($orderBy)) {
			$orderBy = ' ORDER BY `rp`.`id`';
		}

		$sql = "SELECT *, `rp`.`id` AS `rp_id`
				FROM ?f AS `rp`
				INNER JOIN ?f AS `pr` ON `pr`.`id` = `rp`.`rp_project_id`
				LEFT JOIN ?f AS `u` ON `u`.`id` = `rp`.`rp_user_id` AND `u`.`deleted` = 0
				{$joinSearch}
				WHERE `rp`.`deleted` = 0 AND `pr`.`deleted` = 0 AND {$sqlSearch} {$orderBy} {$limit};";


		return self::$DB->select($sql, self::$tableName, TAB_PROJECTS, TAB_USERS);
	}

	/**
	 * @static
	 * @param string $sqlCondition
	 * @return mixed
	 */
	public static function getTotalReportedProblems($sqlCondition = '1=1') {
		$joinSearch = null;
		if (empty($sqlCondition)) {
			$sqlCondition = '1=1';
			$joinSearch = '';
		} else {
		//	$joinSearch = " LEFT JOIN " . TAB_TRANSLATIONS . " tr ON tr.t_module = pr.pr_module AND tr.t_key = pr.pr_name AND tr.t_lang_id = " . conf('COUNTRY_LANG_ID') ;

		//	$sqlCondition .= " OR (`tr`.`t_content` LIKE " . self::$DB->escapeValue('%' . str_replace('%', "\\%", post('sSearch')) . '%') . ")";
		}
		$sql = "SELECT COUNT(*) AS `count_items`
				FROM ?f AS `rp`
				INNER JOIN ?f AS `pr` ON `pr`.`id` = `rp`.`rp_project_id`
				LEFT JOIN ?f AS `u` ON `u`.`id` = `rp`.`rp_user_id` AND `u`.`deleted` = 0
				{$joinSearch}
				WHERE `rp`.`deleted` = 0 AND `pr`.`deleted` = 0 AND {$sqlCondition};";

		return self::$DB->selectCell($sql, self::$tableName, TAB_PROJECTS, TAB_USERS);
	}

	/**
	 * @static
	 * @param $reportedProblemId
	 * @return mixed
	 */
	public static function getReportedProblemById($reportedProblemId) {
		$sql = "SELECT *, `rp`.`id` AS `rp_id`
				FROM ?f AS `rp`
				INNER JOIN ?f AS `pr` ON `pr`.`id` = `rp`.`rp_project_id`
				WHERE `rp`.`deleted` = 0 AND `pr`.`deleted` = 0 AND `rp`.`id` = ?;";
		return self::$DB->selectRow($sql, self::$tableName, TAB_PROJECTS, $reportedProblemId);
	}


}
